package com.itc.bi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.bi.entity.BigScreenImageEntity;
import com.itc.bi.vo.bigScreenVO.ProDirDataSetVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author hecai
* @description 针对表【big_screen_image(上传图片记录表)】的数据库操作Mapper
* @createDate 2024-07-01 21:04:51
* @Entity com.itc.bi.BigScreenImage
*/
public interface BigScreenImageMapper extends BaseMapper<BigScreenImageEntity> {

    /**
     * 获取所有目录数据集
     */
    @Select(" SELECT " +
            " T.*  " +
            "FROM " +
            " ( " +
            " SELECT " +
            "  a.dt_id AS id, " +
            "  a.name AS dataSetName, " +
            "  a.table_name, " +
            "  b.type  " +
            " FROM " +
            "  tb_directory AS a " +
            "  INNER JOIN tb_direct_data_set AS b ON a.dt_id = b.id  " +
            " WHERE " +
            "  a.is_dir = 1 AND a.is_deleted=0  " +
            "  AND a.table_name IS NOT NULL UNION ALL " +
            " SELECT " +
            "  c.id AS id, " +
            "  c.name AS dataSetName, " +
            "  c.table_name, " +
            " CASE " +
            "   c.type  " +
            "   WHEN 'process' THEN " +
            "   '数据加工'  " +
            "   WHEN 'aggregate' THEN " +
            "   '指标聚合' ELSE '' END as type  " +
            "  FROM " +
            "  tb_data_process AS c where c.table_name is not null   " +
            " ) AS T " +
            " where T.dataSetName like '%${dataSetName}%' ")
    Page<ProDirDataSetVO> getProDirDataSetPage(Page<ProDirDataSetVO> page,
                                               @Param("dataSetName")String dataSetName);

    /**
     * 根据id获取唯一值
     * @param id
     * @return
     */
    @Select(" SELECT " +
            " T.*  " +
            "FROM " +
            " ( " +
            " SELECT " +
            "  a.dt_id AS id, " +
            "  a.name AS dataSetName, " +
            "  a.table_name, " +
            "  b.type  " +
            " FROM " +
            "  tb_directory AS a " +
            "  INNER JOIN tb_direct_data_set AS b ON a.dt_id = b.id  " +
            " WHERE " +
            "  a.is_dir = 1  " +
            "  AND a.table_name IS NOT NULL UNION ALL " +
            " SELECT " +
            "  c.id AS id, " +
            "  c.name AS dataSetName, " +
            "  c.table_name, " +
            " CASE " +
            "   c.type  " +
            "   WHEN 'process' THEN " +
            "   '数据加工'  " +
            "   WHEN 'aggregate' THEN " +
            "   '指标聚合' ELSE '' END as type  " +
            "  FROM " +
            "  tb_data_process AS c where c.table_name is not null  " +
            " ) AS T " +
            " where T.id = ${id}")
    ProDirDataSetVO getProDirDataSetById(@Param("id")Long id);

    /**
     * 根据ids获取数据集
     * @param id
     * @return
     */
    @Select(" SELECT " +
            " T.*  " +
            "FROM " +
            " ( " +
            " SELECT " +
            "  a.dt_id AS id, " +
            "  a.name AS dataSetName, " +
            "  a.table_name, " +
            "  b.type  " +
            " FROM " +
            "  tb_directory AS a " +
            "  INNER JOIN tb_direct_data_set AS b ON a.dt_id = b.id  " +
            " WHERE " +
            "  a.is_dir = 1  " +
            "  AND a.table_name IS NOT NULL UNION ALL " +
            " SELECT " +
            "  c.id AS id, " +
            "  c.name AS dataSetName, " +
            "  c.table_name, " +
            " CASE " +
            "   c.type  " +
            "   WHEN 'process' THEN " +
            "   '数据加工'  " +
            "   WHEN 'aggregate' THEN " +
            "   '指标聚合' ELSE '' END as type  " +
            "  FROM " +
            "  tb_data_process AS c where c.table_name is not null  " +
            " ) AS T " +
            " where T.id in (${ids})")
    List<ProDirDataSetVO> getProDirDataSetByIds(@Param("ids")String ids);
}
